package com.airlines.crewassignment.service.rule.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.airlines.crewassignment.constant.CrewConstant;
import com.airlines.crewassignment.dao.AssignmentRuleMapper;
import com.airlines.crewassignment.domain.rule.AssignmentRule;
import com.airlines.crewassignment.service.rule.AssignmentRuleService;

/**
 * @author xxxx
 * @date 2018年3月29日 
 * @description
 */
@Service
public class AssignmentRuleServiceImpl implements AssignmentRuleService {
	@Autowired
	private AssignmentRuleMapper assignmentRuleMapper;
	
	@Override
    public int insert(AssignmentRule assignmentRule){
		assignmentRule.setIsDelete(0);
		AssignmentRule other = this.get(assignmentRule.getRuleCode());
		if(other != null){
			return 0;
		}else{
			return assignmentRuleMapper.insert(assignmentRule);
		}
    }

    @Override
    public int deleteLogic(Integer ruleId){
        return assignmentRuleMapper.deleteLogic(ruleId);
    }

    @Override
    public int update(AssignmentRule assignmentRule){
    	AssignmentRule other = this.get(assignmentRule.getRuleCode());
    	if(other.getRuleId().equals(assignmentRule.getRuleId())){
    		return assignmentRuleMapper.update(assignmentRule);
    	}else{
    		return 0;
    	}     
    }

    @Override
    public AssignmentRule get(Integer ruleId){
        return assignmentRuleMapper.get(ruleId);
    }

    @Override
    public List<AssignmentRule> list(AssignmentRule assignmentRule){
        return assignmentRuleMapper.list(assignmentRule);
    }

    @Override
    public List<AssignmentRule> listByFuzzy(AssignmentRule assignmentRule){
        return assignmentRuleMapper.listByFuzzy(assignmentRule);
    }

	@Override
	public AssignmentRule get(String ruleCode) {
		AssignmentRule assignmentRule = new AssignmentRule();
		assignmentRule.setRuleCode(ruleCode);
		List<AssignmentRule> list  = this.list(assignmentRule);
		if(list != null && list.size()==1){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<AssignmentRule> listAll(String dutyCode) {
		AssignmentRule assignmentRule = new AssignmentRule();
		assignmentRule.setDutyCode(dutyCode);
		assignmentRule.setIsEnabled(1);
		return this.list(assignmentRule);
	}

	@Override
	public boolean assignmentRuleAbled(AssignmentRule assignmentRule) {
		if(assignmentRule == null){
			return false;
		}else if(assignmentRule.getIsDelete() ==1){
			return false;
		}else if(assignmentRule.getIsEnabled() ==0){
			return false;
		}
		return true;
	}

	@Override
	public int disable(Integer ruleId) {
		AssignmentRule assignmentRule = new AssignmentRule();
		assignmentRule.setRuleId(ruleId);
		assignmentRule.setIsEnabled(0);
		return assignmentRuleMapper.update(assignmentRule);
	}
	
	@Override
	public int able(Integer ruleId) {
		AssignmentRule assignmentRule = new AssignmentRule();
		assignmentRule.setRuleId(ruleId);
		assignmentRule.setIsEnabled(1);
		return assignmentRuleMapper.update(assignmentRule);
	}
}
